package com.yanxinim.cn.store;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.yanxinim.cn.AppConfig;
import com.yanxinim.cn.R;
import com.yanxinim.cn.store.bean.StoreAddressInfo;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.ToastUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class StoreCreateAddressActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_user_name)
    EditText etUserName;
    @BindView(R.id.et_user_phone)
    EditText etUserPhone;
    @BindView(R.id.et_user_address)
    EditText etUserAddress;
    @BindView(R.id.et_user_address_details)
    EditText etUserAddressDetails;
    @BindView(R.id.tv_save_addreaa)
    TextView tvSaveAddreaa;
    @BindView(R.id.radio_male)
    RadioButton radioMale;
    @BindView(R.id.radio_female)
    RadioButton radioFemale;
    @BindView(R.id.radio_group_sex)
    RadioGroup radioGroupSex;
    @BindView(R.id.radio_yes)
    RadioButton radioYes;
    @BindView(R.id.radio_no)
    RadioButton radioNo;
    @BindView(R.id.radio_group_default)
    RadioGroup radioGroupDefault;
    @BindView(R.id.tv_del_address)
    TextView tvDelAddress;
    private String sex = "1";

    String userName;
    String userPhone;
    String addressText;
    String AddressCode;
    String addressDefault = "1";

    private StoreAddressInfo storeAddressInfo;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_store_create_address);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        if (type == 2) {
            storeAddressInfo = (StoreAddressInfo) getIntent().getSerializableExtra("addressInfo");
            etUserName.setText(storeAddressInfo.getUserName());
            etUserPhone.setText(storeAddressInfo.getUserPhone());
            etUserAddress.setText(storeAddressInfo.getAddressText());
            etUserAddressDetails.setText(storeAddressInfo.getAddressCode());
            addressDefault = storeAddressInfo.getAddressDefault();
            sex = storeAddressInfo.getUserSex();
            if (sex.equals("1")) {
                radioGroupSex.check(R.id.radio_female);
            } else {
                radioGroupSex.check(R.id.radio_male);
            }
            if (addressDefault.equals("1")) {
                radioGroupDefault.check(R.id.radio_yes);
            } else {
                radioGroupDefault.check(R.id.radio_no);
            }

            tvDelAddress.setVisibility(View.VISIBLE);
        } else {
            radioGroupSex.check(R.id.radio_male);
            radioGroupDefault.check(R.id.radio_yes);
            tvDelAddress.setVisibility(View.GONE);
        }
        radioGroupSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_male)
                    sex = "1";
                else sex = "2";
            }
        });
        radioGroupDefault.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_no)
                    addressDefault = "0";
                else addressDefault = "1";
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_save_addreaa, R.id.tv_del_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_del_address:
                deleteAddress();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save_addreaa:
                check();
                break;
        }
    }

    private void deleteAddress() {

        HttpUtils.post().url(AppConfig.STORE_DELETE_ADDRESS)
                .params("addressId", storeAddressInfo.getId())
                .build()
                .execute(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(result);
                        Log.i("qin", result);
                        if (jsonObject.getInteger("resultCode") == 1) {
                            finish();
                        }

                    }
                });
    }

    private void check() {
        userName = etUserName.getText().toString().trim();
        userPhone = etUserPhone.getText().toString().trim();
        addressText = etUserAddress.getText().toString().trim();
        AddressCode = etUserAddressDetails.getText().toString().trim();

        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showToast(StoreCreateAddressActivity.this, "请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(userPhone)) {
            ToastUtil.showToast(StoreCreateAddressActivity.this, "请输入用户电话");
            return;
        }
        if (TextUtils.isEmpty(addressText)) {
            ToastUtil.showToast(StoreCreateAddressActivity.this, "请输入地址");
            return;
        }
        if (TextUtils.isEmpty(AddressCode)) {
            ToastUtil.showToast(StoreCreateAddressActivity.this, "请输入详细地址");
            return;
        }
        if (type == 2) {
            updateAddress();
        } else {
            createAddress();
        }
    }

    private void updateAddress() {
        Map<String, String> param = new HashMap<>();
        param.put("addressId", storeAddressInfo.getId());
        param.put("userId", getUser().getUserId());
        param.put("userName", userName);
        param.put("userSex", sex);
        param.put("addressDefault", addressDefault);
        param.put("userPhone", userPhone);
        param.put("addressText", addressText);
        param.put("addressCode", AddressCode);

        HttpUtils.post().url(AppConfig.STORE_UPDATE_ADDRESS)
                .params(param)
                .build()
                .execute(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(result);
                        Log.i("qin", result);
                        if (jsonObject.getInteger("resultCode") == 1) {
                            finish();
                        }

                    }
                });
    }

    private void createAddress() {
        Map<String, String> param = new HashMap<>();
        param.put("userId", getUser().getUserId());
        param.put("userName", userName);
        param.put("userSex", sex);
        param.put("addressDefault", addressDefault);
        param.put("userPhone", userPhone);
        param.put("addressText", addressText);
        param.put("addressCode", AddressCode);

        HttpUtils.post().url(AppConfig.STORE_CREATE_ADDRESS)
                .params(param)
                .build()
                .execute(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(result);
                        Log.i("qin", result);
                        if (jsonObject.getInteger("resultCode") == 1) {
                            finish();
                        }

                    }
                });
    }
}
